<?php
$this->widget('application.extensions.fancybox.EFancyBox', array('target' => '.product', 'config' => array('width' => 800, 'height' => 640, 'type' => 'iframe', 'hideOnOverlayClick' => false, 'onClosed' => 'js:function(){window.location.href="' . Yii::app()->createUrl("psi/trackingUpdate", array("trackingIndex" => $model->TrackingIndex)) . '";}')));
$this->breadcrumbs = array(
    '已有运输记录' => array('tracking'),
);
include_once('menu.php');
if ($model->isNewRecord) {
    $title = '新增运输记录';
} else {
    $title = '更新运输记录';
}
?>

<h1><?= $title ?></h1>
<div style="width:100%; float:left; margin-bottom:10px; text-align:right;">
    <?php
    $form = $this->beginWidget('CActiveForm', array(
        'id' => 'tracking-form',
        'htmlOptions' => array('name' => 'jobForm', 'onsubmit' => 'return fxl();'),
        'enableAjaxValidation' => false,
            ));
    ?>
        <?php echo $form->errorSummary($model); ?>

    <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <?php
        if ($model->ShippingStatus == 'DataLogging') {
            ?>
            <tr>
                <td width="15%" class="td_right"><?php echo $form->label($model, 'CarrierNo'); ?>: </td>
                <td width="35%">				
    <?php echo $form->textField($model, 'CarrierNo', array('class' => 'input_size4')); ?>
                </td>
                <td width="15%" class="td_right"><?php echo $form->label($model, 'EBoxNumber'); ?>: </td>
                <td>
    <?php echo $form->textField($model, 'EBoxNumber', array('class' => 'input_size4')); ?>
                </td>
            </tr>
            <tr>
                <td class="td_right"><?php echo $form->label($model, 'ShippingStatus'); ?>: </td>
                <td>
    <?php echo $model->ShippingStatus; ?>
                </td>
                <td class="td_right"><?php echo $form->label($model, 'ShippingType'); ?>: </td>			
                <td>
    <?php echo $form->dropDownList($model, 'ShippingType', Yii::app()->params['shippingType'], array('class' => 'input_size6')); ?>
                </td>
            </tr>
            <tr>
                <td class="td_right"><?php echo $form->label($model, 'ShipDate'); ?>: </td>
                <td>
                    <?php
                    $this->widget('zii.widgets.jui.CJuiDatePicker', array(
                        'attribute' => 'ShipDate',
                        'model' => $model,
                        'language' => 'zh',
                        'options' => array(
                            'showAnim' => 'fold',
                            'dateFormat' => 'yymmdd',
                        ),
                        'htmlOptions' => array(
                            'readonly' => 'readonly',
                            'class' => 'input_size4'
                        ),
                    ));
                    ?>
                </td>
                <td class="td_right"><?php echo $form->label($model, 'ArriveDate'); ?>: </td>			
                <td>
                    <?php
                    $this->widget('zii.widgets.jui.CJuiDatePicker', array(
                        'attribute' => 'ArriveDate',
                        'model' => $model,
                        'language' => 'zh',
                        'options' => array(
                            'showAnim' => 'fold',
                            'dateFormat' => 'yymmdd',
                        ),
                        'htmlOptions' => array(
                            'readonly' => 'readonly',
                            'class' => 'input_size4'
                        ),
                    ));
                    ?>
                </td>
            </tr>
    <?php
} else {
    ?>
            <tr>
                <td width="15%" class="td_right"><?php echo $form->label($model, 'CarrierNo'); ?>: </td>
                <td width="35%">				
                    <?php echo $model->CarrierNo; ?>
                </td>
                <td width="15%" class="td_right"><?php echo $form->label($model, 'EBoxNumber'); ?>: </td>
                <td>
    <?php echo $model->EBoxNumber; ?>
                </td>
            </tr>
            <tr>
                <td class="td_right"><?php echo $form->label($model, 'ShippingStatus'); ?>: </td>
                <td>
                    <?php echo $model->ShippingStatus; ?>
                </td>
                <td class="td_right"><?php echo $form->label($model, 'ShippingType'); ?>: </td>			
                <td>
    <?php echo $model->ShippingType; ?>
                </td>
            </tr>
            <tr>
                <td class="td_right"><?php echo $form->label($model, 'ShipDate'); ?>: </td>
                <td>
                    <?php echo $model->ShipDate; ?>
                </td>
                <td class="td_right"><?php echo $form->label($model, 'ArriveDate'); ?>: </td>			
                <td>
                    <?php
                    $this->widget('zii.widgets.jui.CJuiDatePicker', array(
                        'attribute' => 'ArriveDate',
                        'model' => $model,
                        'language' => 'zh',
                        'options' => array(
                            'showAnim' => 'fold',
                            'dateFormat' => 'yymmdd',
                        ),
                        'htmlOptions' => array(
                            'readonly' => 'readonly',
                            'class' => 'input_size4'
                        ),
                    ));
                    ?>
                </td>
            </tr>
                    <?php
                }
                ?>		
        <tr>
            <td class="td_right"><?php echo $form->label($model, 'OETrackingStatus'); ?>: </td>
            <td colspan="3">
                <?php echo $form->textField($model, 'OETrackingStatus', array('class' => 'input_size3')); ?>
            </td>			
        </tr>
        <tr>
            <td class="td_right"><?php echo $form->label($model, 'Comment'); ?>: </td>
            <td colspan="3">
                <?php
                $this->widget('application.extensions.kindeditor4.KindEditorWidget', array('model' => $model, 'attribute' => 'Comment',
                    'htmlOptions' => array('width' => '100%'),
                    'miniMode' => true, 'editorname' => 'Description'));
                ?>
            </td>			
        </tr>		
    </table>
    <?php
    if ($model->TrackingIndex) {
        if ($model->ShippingStatus == 'DataLogging') {
            ?>
            <div style="text-align:right; padding:10px 10px 0 0; font-weight:bold;"><a id="item" class='product' href="<?= Yii::app()->createUrl('psi/addTrackingItem', array('trackingIndex' => $model->TrackingIndex)) ?>">关联产品</a></div>
            <?php
        }
        ?>
    <?php
    $rowTable = $model->mids;
    if ($rowTable) {
        ?>
            <table class="detail-view" style="margin-top:10px;">
                <tbody>
                    <tr class="head">
                        <td width="15%" style="text-align:center;">产品编号</td>
                        <td style="text-align:center;">产品细节</td>
                        <td width="8%" style="text-align:center;">订购数量</td>
                        <td width="8%" style="text-align:center;">未出货数量</td>
                        <td width="10%" style="text-align:center;">运出数量</td>
                        <?php
                        if ($model->ShippingType == 'Container') {
                            ?>
                            <td width="15%" style="text-align:center;">海运箱号</td>
                            <?php
                        }
                        ?>
                        <?php
                        if ($model->ShippingStatus == 'DataLogging') {
                            ?>
                            <td width="8%" style="text-align:center;">删除</td>
                        <?php
                    }
                    ?>			
                    </tr>
                    <?php
                    $i = 0;
                    $cssArr = array(0 => 'odd', 1 => 'even');
                    $allNum = 0;
                    foreach ($rowTable as $row) {
                        $trClass = $cssArr[$i % 2];
                        $i++;
                        $allNum+=$row->ToLAQty;
                        ?>
                        <tr class="<?= $trClass ?>">
                            <td><?= $row->pricing->ProductModel ?></td>
                            <td><?= $row->pricing->ProductDetail ?></td>
                            <td style="text-align:center;"><?= $row->pricing->PQuantity ?></td>
                            <td style="text-align:center;"><?= $row->pricing->Balance - $row->pricing->ToLAQty ?></td>			
                                <?php
                                if ($model->ShippingStatus == 'DataLogging') {
                                    ?>
                                <td style="text-align:center;">
                                <?php echo CHtml::textField('ToLAQty_' . $row->MidIndex, $row->ToLAQty, array('style' => 'width:40px;')); ?>
                                </td>
                                    <?php
                                    if ($model->ShippingType == 'Container') {
                                        ?>
                                    <td style="text-align:center;">
                                    <?php echo CHtml::textField('BoxNum_' . $row->MidIndex, $row->CBoxNumber, array('style' => 'width:80px;')); ?>
                                    </td>
                                    <?php
                                }
                                ?>		
                                <td style="text-align:center;"><a class="fan1" href="<?= Yii::app()->createUrl('psi/deleteTrackingItem', array('id' => $row->MidIndex)) ?>">删除</a></td>
                                    <?php
                                } else {
                                    ?>
                                <td style="text-align:center;">
                                <?php echo $row->ToLAQty; ?>
                                </td>
                                    <?php
                                    if ($model->ShippingType == 'Container') {
                                        ?>
                                    <td style="text-align:center;">
                                    <?php echo $row->CBoxNumber; ?>
                                    </td>
                                    <?php
                                }
                                ?>		
                            <?php
                        }
                        ?>		

                        </tr>
                <?php
            }
            ?>
                </tbody>
            </table>
            <?php
        }
        ?>
            <?php
        }
        ?>
    <div style="text-align:right; padding:10px 10px 0 0; color:red;">共有记录：<?=count($rowTable)?>条，总数量：<?=(int)$allNum?></div>
    <div style="text-align:center;">
    <?php
    echo CHtml::submitButton($model->isNewRecord ? '新建' : '更新');
    ?>
    </div>
<?php $this->endWidget(); ?>
</div>
<script type="text/javascript">
    /*<![CDATA[*/
    function fxl() {
        if ($('#Tracking_CarrierNo').val() == '' && $('#Tracking_EBoxNumber').val() == '') {
            alert('货柜编号和箱号必须填写一个')
            return false;
        }
<?php
foreach ($rowTable as $item) {
    ?>
            if (parseInt($('#ToLAQty_<?= $item->MidIndex ?>').val()) > parseInt('<?= Helper::getAbleNumByPricingIndex($item->PricingIndex) ?>') +<?= (int) $item->ToLAQty ?>) {
                $('#ToLAQty_<?= $item->MidIndex ?>').focus();
                alert('运出数量不能大于可运出数量')
                return false;
            }
            if (parseInt($('#ToLAQty_<?= $item->MidIndex ?>').val()) <= 0 || $('#ToLAQty_<?= $item->MidIndex ?>').val() == '') {
                $('#ToLAQty_<?= $item->MidIndex ?>').focus();
                alert('请输入运出数量')
                return false;
            }
    <?php
}
?>
        return true;
    }
    /*]]>*/
</script>
